<template>
  <div class="base-container">
    <t-form ref="form" :data="queryParams" label-width="80" @submit="()=>{this.pagination.current=1;this.queryParams.pageNum=1;this.getList()}" @reset="()=>{this.pagination.current=1;this.queryParams.pageNum=1;this.getList()}">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
        <t-col :span="11" class="query-form-inline">
          <t-form-item label="平台名称" name="name">
            <t-input
              v-model="queryParams.name"
              clearable
              placeholder="请输入平台名称"
              style="width: 240px"
            />
          </t-form-item>
          <t-form-item label="平台类别" name="platformCategory">
            <t-select  style="width: 240px" clearable v-model="queryParams.platformCategory"
                       :options="dict.type.platform_category">
            </t-select>
          </t-form-item>
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit"> 查询</t-button>
          <t-button theme="default" variant="base" type="reset">重置</t-button>
        </t-col>
      </t-row>
    </t-form>
    <t-table
      rowKey="index"
      size="small"
      :data="data"
      :columns="columns"
      hover
      :pagination="pagination"
      :loading="dataLoading"
      :sort="sort"
      :hideSortTips="true"
      @sort-change="sortChange"
    >
      <template #name="{ row }">
        <t-link @click="dialog.objId=row.id;dialog.header=row.name;dialog.open_project_detail=true" hover="color" theme="primary">{{row.name}}</t-link>
      </template>
      <template #platformCategory="{ row }">
        {{ row.platformCategory | filterByDict(dict.type.platform_category) }}
      </template>
      <template #inputCount="{ row }">
        <t-link @click="nameClick(row)" hover="color" theme="primary">{{row.inputCount}}</t-link>
      </template>
      <!-- <template #op="{ row }">
       <t-button size="small" @click="nameClick(row)" variant="text" class="t-button-link">
         <t-icon name="info-circle" slot="icon"/>
         详情
       </t-button>
        <t-button
          @click="apply(row)"
          size="small"
          variant="text" class="t-button-link">
          <t-icon name="edit-1" slot="icon"/>
          新增设备
        </t-button>
      </template> -->
    </t-table>
    <t-dialog :visible.sync="dialogInfo.visible" :header="dialogInfo.header" :cancel-btn="null" :confirmBtn="null" :onConfirm="onConfirm" width="40%" top="5%">
      <t-form ref="form" class="step-form" :data="formData" @submit="onSubmit" :rules="rules" labelWidth="180px">
        <t-form-item label="投入年份：" name="year">
          <t-date-picker mode="year" style="width: 390px"  v-model="formData.year"></t-date-picker>
        </t-form-item>
        <t-form-item label="设备名称：" name="name">
          <t-input style="width: 390px" clearable v-model="formData.name"
          ></t-input>
        </t-form-item>
        <t-form-item label="型号：" name="model">
          <t-input style="width: 390px" clearable v-model="formData.model"
          ></t-input>
        </t-form-item>
        <t-form-item label="数量：" name="count">
          <t-input style="width: 390px"  clearable v-model="formData.count"
          ></t-input>
        </t-form-item>
        <t-form-item label="主要用途：" name="mainUse">
          <t-input style="width: 390px"  clearable v-model="formData.mainUse"
          ></t-input>
        </t-form-item>
        <t-form-item label="添置方式：" name="addMethod">
          <t-select style="width: 240px" clearable v-model="formData.addMethod"
                    :options="dict.type.add_method">
          </t-select>
        </t-form-item>
        <t-form-item label="单价(万元)：" name="price">
          <t-input style="width: 390px"  clearable v-model="formData.price"
          ></t-input>
        </t-form-item>
        <t-form-item label="研发场地面面积(m2)：" name="siteArea">
          <t-input style="width: 390px"  clearable v-model="formData.siteArea"
          ></t-input>
        </t-form-item>
        <t-form-item label="备注：" name="note">
          <t-textarea style="width: 390px" clearable v-model="formData.note"></t-textarea>
          <!--          <t-input style="width: 390px" clearable v-model="formData.note"></t-input>-->
        </t-form-item>
        <div style="height: 10px"></div>
        <t-button style="float: right;" :loading="subBtnLoading" theme="primary"  type="submit">提交</t-button>
        <t-button style="float: right; margin-right: 10px;" :loading="subBtnLoading" theme="primary"  @click="cancelClick">取消</t-button>
      </t-form>
    </t-dialog>
    <equipmentInfo @edit="edit" :is-list="false" @closeBzhDialog="closeBzhDialog" v-if="detailDialog.visible" :dialogInfo="detailDialog"></equipmentInfo>
    <platform-detail-dialog :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_project_detail" v-if="dialog.open_project_detail"></platform-detail-dialog>

  </div>
</template>

<script>
import equipmentInfo from "@/pages/kjpt/components/equipment-info.vue";
import platformDetailDialog from "@/pages/kjpt/platform/components/platform-detail-dialog.vue";

export default {
  name: "myList",
  dicts: ["platform_category","add_method"],
  components: {
    equipmentInfo,
    platformDetailDialog
  },
  data() {
    return {
      isAdd:true,
      isEdit:false,
      detailDialog:{
        id:"",
        header:"",
        visible: false,
      },
      subBtnLoading:false,
      rules: {
        year:[
          {
            required: true,
            message: "投入年份不能为空",
          },
        ],
        name:[
          {
            required: true,
            message: "设备名称不能为空",
          },
        ],
        model:[
          {
            required: true,
            message: "型号不能为空",
          },
        ],
        count:[
          {
            required: true,
            message: "数量不能为空",
          },
        ],
        mainUse:[
          {
            required: true,
            message: "主要用途不能为空",
          },
        ],
        addMethod:[
          {
            required: true,
            message: "添置方式不能为空",
          },
        ],
        price:[
          {
            required: true,
            message: "单价(万元)不能为空",
          },
        ],
        siteArea:[
          {
            required: true,
            message: "研发场地面面积(m2)不能为空",
          },
        ],
      },
      formData:{
        inputDate:"",
        name:"",
        model:"",
        count:"",
        mainUse:"",
        addMethod:"",
        price:"",
        siteArea:"",
        note:"",
      },
      dialogInfo:{
        visible:false,
        header:"",
      },
      nowRow:"",
      sort:[],
      dialog: {
        open_project_detail: false,
        header: "",
        objId: 0,
        open_approve_list: false,
        open_project_file: false
      },
      dictTypes: {},
      projectCategoryOptions: [],
      columns:[
        {
          colKey: "name",
          title: "平台名称",
          align: "center",
          width: 220

        },
        {
          colKey: "count",
          title: "总投入设备数量",
          align: "center",
          width: 180
        },
        {
          colKey: "totalMoney",
          title: "总投入设备金额（万元）",
          align: "center",
          width: 180
        },
        {
          colKey: "siteArea",
          title: "总投入研发场地面积（m2）",
          align: "center",
          width: 180
        },
        {
          colKey: "inputCount",
          title: "投入次数",
          align: "center",
          width: 180
        },
        // {
        //   colKey: "op",
        //   title: "操作",
        //   align: "center",
        //   width: 120,
        //   fixed: 'right'
        // },
      ],
      data: [],
      dataLoading: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name:"",
        platformCategory:""
      },
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10, 20, 30, 50],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination,pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      },
    }
  },
  async created() {
    this.getList();
  },
  methods: {
    cancelClick(){
      this.dialogInfo.visible = false
      if(!this.isAdd){
        this.detailDialog.visible = true
      }
    },
    edit(row){
      this.isAdd = false
      this.nowRow = row
      this.isEdit = true
      this.$api.kjpt.equipment.get(row.id).then(response=>{
        this.formData = response.data
        if (!this.formData.note){
          this.formData.note= "无"
        }
        this.dialogInfo.visible = true
        this.detailDialog.visible =false
      })
    },
    // 点击列表名字事件
    nameClick(row){
      this.detailDialog.id = row.id
      this.detailDialog.header = row.name
      this.detailDialog.visible = true
    },
    // 关闭详情弹框
    closeBzhDialog(data){
      this.detailDialog.visible = data
    },
    changMoney(){
      this.formData.totalBudget =Number(this.formData.zcBudget)+Number(this.formData.tbBudget)
    },
    onSubmit({ validateResult, firstError }) {
      if (validateResult === true) {
        this.subBtnLoading = true
        if(this.isEdit){
          this.formData.platformId = this.nowRow.platformId
        }else{
          this.formData.platformId = this.nowRow.id
        }
        delete this.formData.totalBudget
        if(this.isEdit){
          this.$api.kjpt.equipment.edit(this.formData).then(response => {
            this.subBtnLoading =false
            this.$message.success("修改成功")
            this.$refs.form.reset()
            this.dialogInfo.visible = false
            this.detailDialog.visible = true
            this.getList()
          }).catch(e => {
            this.subBtnLoading =false
            this.$message.error(e.toString())
            this.$refs.form.reset()
            this.dialogInfo.visible = false
          })
        }else{
          this.$api.kjpt.equipment.add(this.formData).then(response => {
            this.subBtnLoading =false
            this.$message.success("新增成功")
            this.$refs.form.reset()
            this.dialogInfo.visible = false
            this.getList()
          }).catch(e => {
            this.subBtnLoading =false
            this.$message.error(e.toString())
            this.$refs.form.reset()
            this.dialogInfo.visible = false
          })
        }

      }else{
        this.subBtnLoading =false
        this.$message.error(firstError)
      }
    },
    // onSubmit({ validateResult, firstError }) {
    //   if (validateResult === true) {
    //     this.subBtnLoading = true
    //     this.formData.platformId = this.nowRow.id
    //     delete this.formData.totalBudget
    //     this.$api.kjpt.equipment.add(this.formData).then(response => {
    //       this.subBtnLoading =false
    //       this.$message.success("新增成功")
    //       this.$refs.form.reset()
    //       this.dialogInfo.visible = false
    //       this.getList()
    //     }).catch(e => {
    //       this.subBtnLoading =false
    //       this.$message.error(e.toString())
    //       this.$refs.form.reset()
    //       this.dialogInfo.visible = false
    //     })
    //   }else{
    //     this.subBtnLoading =false
    //     this.$message.error(firstError)
    //   }
    // },
    onConfirm(){

    },
    sortChange(sort){
      this.sort = sort;
      let orderByColumn = []
      let isAsc = []
      if(sort instanceof Array){
        orderByColumn = sort.map(val=>val.sortBy)
        isAsc = sort.map(val=>val.descending?`desc`:`asc`)
      }else {
        orderByColumn = [sort.sortBy]
        isAsc = [sort.descending?`desc`:`asc`]
      }
      this.queryParams.orderByColumn = orderByColumn.join(",")
      this.queryParams.isAsc = isAsc.join(",")
      this.getList();
    },
    getList() {
      this.dataLoading = true;
      this.queryParams.pageNum = this.pagination.current || 1;
      this.$api.kjpt.equipment.myList(this.queryParams).then(response => {
        this.pagination.total = response.total;
        this.data = response.rows;
        this.dataLoading = false;
      }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },
    apply(row) {
      this.isAdd = true
      this.nowRow = row
      this.dialogInfo.visible = true
      this.dialogInfo.header = row.name
      this.isEdit = false
    },
    deleteProject(row) {
      const confirmDia = this.$dialog.confirm({
        header: "系统提示",
        body: `是否确认删除项目名称为"${row.projectName}"的数据项？`,
        theme: "warning",
        onConfirm: ({e}) => {
          confirmDia.update({
            confirmBtn: {
              loading: true,
            },
          })
          this.$api.kjxm.project.del(row.id).then(
            (res) => {
              this.getList();
              this.$message.success("删除成功");
              // 请求成功后，销毁弹框
              confirmDia.destroy();
            }
          ).catch((e) => {
            confirmDia.update({
              confirmBtn: {
                loading: false,
              },
            })
            this.$message.error(e.toString());
          });
        }
      });
    },
  }
}
</script>

<style scoped>

</style>
